//
//  LMNewFeatures.swift
//  WeiBo
//
//  Created by Li MAc on 2018/11/27.
//  Copyright © 2018 Li MAc. All rights reserved.
//

import UIKit

class LMNewFeatures: UIView {

    @IBOutlet weak var scr: UIScrollView!
    
    @IBOutlet weak var but: UIButton!
    
    @IBAction func toupbut(_ sender: Any) {
        
    }
    

    
    @IBOutlet weak var page: UIPageControl!
    
    class func newFeatures() -> LMNewFeatures {
        let nib = UINib(nibName: "NewFeatures", bundle:nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! LMNewFeatures
        v.frame = UIScreen.main.bounds
        
        
        return v
    }
    
    override func awakeFromNib() {
        let count = 4
        let rect = UIScreen.main.bounds
        
        for i in 0..<count{
            let imgname = "new_feature_\(i+1)"
            let img = UIImageView(image: UIImage(named: imgname))
             img.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            scr.addSubview(img)
        }
        scr.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        //弹回
        scr.bounces = false
        //整页平移是否开启
        scr.isPagingEnabled = true
        
        scr.showsVerticalScrollIndicator = false
        scr.showsHorizontalScrollIndicator = false
        
        scr.delegate = self
        
        but.isHidden = true
    }

}


extension LMNewFeatures:UIScrollViewDelegate{
    
    func scrollViewDidScroll(_ scrollView: UIScrollView){
        //f滚动就隐藏按钮
        but.isHidden = true
        
        //计算偏移量
        let inttt = Int(scr.contentOffset.x / scr.bounds.width + 0.5)
        
        page.currentPage = inttt
    }
    
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
        
        let pa = Int(scr.contentOffset.x / scrollView.bounds.width)
        
        if pa == scr.subviews.count{
            //移除scr
            removeFromSuperview()
        }
        //如果是倒数的二页显示按钮
        but.isHidden = (pa != scr.subviews.count - 1)
    }
}
